package org.unicat.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.unicat.mapper.StudentMapper;
import org.unicat.mapper.TeacherMapper;
import org.unicat.mapper.UserInfoMapper;
import org.unicat.mapper.UserMapper;
import org.unicat.model.Student;
import org.unicat.model.Teacher;
import org.unicat.model.UserInfo;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;

@Order(1)
@Aspect
@Component
public class StudentServiceLogAop {
    @Autowired
    UserInfoMapper userInfoMapper;

    @Autowired
    UserMapper userMapper;

    @Autowired
    StudentMapper studentMapper;

    //查找所有学生
    @Before("execution(* org.unicat.service.StudentService.findAllStudent(..))")
    public void findAllStudentLog(JoinPoint joinPoint) {
        System.out.println("-----------------------------------------------------------");
        Calendar startTimeNow = Calendar.getInstance();
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy年MM月dd日  HH:mm:ss");
        String startTime = fmt.format(startTimeNow.getTime());
        System.out.println(startTime);
        System.out.println("查找所有学生:");
    }

    @AfterReturning(returning = "result", pointcut = "execution(* org.unicat.service.StudentService.findAllStudent(..))")
    public void findAllStudentLog(JoinPoint joinPoint, Object result) {
        System.out.println(Arrays.asList(result));
    }

    //查找一个学生
    @Before("execution(* org.unicat.service.StudentService.findStudentByName(..))")
    public void findOneStudentLog(JoinPoint joinPoint) {

        System.out.println("-----------------------------------------------------------");

        Calendar startTimeNow = Calendar.getInstance();
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy年MM月dd日  HH:mm:ss");
        String startTime = fmt.format(startTimeNow.getTime());
        System.out.println(startTime);

        Object[] args = joinPoint.getArgs();

        System.out.println("查找" + Arrays.asList((String)args[0]) + "学生:");
    }

    @AfterReturning(returning = "result", pointcut = "execution(* org.unicat.service.StudentService.findStudentByName(..))")
    public void findOneStudentLog(JoinPoint joinPoint, Student result) {
        System.out.println(Arrays.asList(result));
    }

    //获取学生信息
    @Before("execution(* org.unicat.service.StudentService.getStudentInfo(..))")
    public void getStudentInfoLog(JoinPoint joinPoint) {

        System.out.println("-----------------------------------------------------------");

        Calendar startTimeNow = Calendar.getInstance();
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy年MM月dd日  HH:mm:ss");
        String startTime = fmt.format(startTimeNow.getTime());
        System.out.println(startTime);

        Object[] args = joinPoint.getArgs();

        System.out.println("获取" + Arrays.asList(((Student) args[0]).getName()) + "学生的信息:");
    }

    @AfterReturning(returning = "result", pointcut = "execution(* org.unicat.service.StudentService.getStudentInfo(..))")
    public void getStudentInfoLog(JoinPoint joinPoint, UserInfo result) {
        System.out.println(Arrays.asList(result));
    }

    //添加老师
    @Before("execution(* org.unicat.service.StudentService.addStudent(..))")
    public void addStudentLog(JoinPoint joinPoint) {

        System.out.println("-----------------------------------------------------------");

        Calendar startTimeNow = Calendar.getInstance();
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy年MM月dd日  HH:mm:ss");
        String startTime = fmt.format(startTimeNow.getTime());
        System.out.println(startTime);

        Object[] args = joinPoint.getArgs();

        System.out.println("添加" + Arrays.asList(((UserInfo) args[0]).getUsername()) + "学生:");
        System.out.println(Arrays.asList(args));
    }

    //更新老师信息
    @Before("execution(* org.unicat.service.StudentService.updateStudentInfo(..))")
    public void updateStudentInfoBeforeLog(JoinPoint joinPoint) {

        System.out.println("-----------------------------------------------------------");

        Calendar startTimeNow = Calendar.getInstance();
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy年MM月dd日  HH:mm:ss");
        String startTime = fmt.format(startTimeNow.getTime());
        System.out.println(startTime);

        Object[] args = joinPoint.getArgs();

        System.out.println("更新前:");
        UserInfo userInfo = userInfoMapper.selectOne(((UserInfo) args[0]).getUsername());
        userInfo.setPassword(userMapper.selectOne(((UserInfo) args[0]).getUsername()).getPassword());
        userInfo.setMajor(studentMapper.selectOne(((UserInfo) args[0]).getUsername()).getMajor());
        System.out.println(Arrays.asList(userInfo));
    }

    @After("execution(* org.unicat.service.StudentService.updateStudentInfo(..))")
    public void updateStudentInfoAfterLog(JoinPoint joinPoint) {
        Object[] args = joinPoint.getArgs();

        System.out.println("更新后:");
        UserInfo userInfo = userInfoMapper.selectOne(((UserInfo) args[0]).getUsername());
        userInfo.setPassword(userMapper.selectOne(((UserInfo) args[0]).getUsername()).getPassword());
        userInfo.setMajor(studentMapper.selectOne(((UserInfo) args[0]).getUsername()).getMajor());
        System.out.println(Arrays.asList(userInfo));

    }

    //删除老师
    @Before("execution(* org.unicat.service.StudentService.deleteStudent(..))")
    public void deleteStudentLog(JoinPoint joinPoint) {

        System.out.println("-----------------------------------------------------------");

        Calendar startTimeNow = Calendar.getInstance();
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy年MM月dd日  HH:mm:ss");
        String startTime = fmt.format(startTimeNow.getTime());
        System.out.println(startTime);

        Object[] args = joinPoint.getArgs();

        System.out.println("删除" + Arrays.asList(((Student) args[0]).getName()) + "学生:");
        System.out.println(Arrays.asList(args));
    }
}
